const fs = require('fs')
const path = require('path')
const db = require('../db/db')
const {paging} = require('../utils/paging')
const {host} = require('../config/config')
module.exports = {
    // 头像上传
    avatarImgUpload: async (ctx,next)=>{
        // 图片地址拼接
        const headPath = host + '/avatarImgs/'+ctx.req.file.filename
        ctx.body = {
            code: 200,
            fileUrl: headPath,
            filename:ctx.req.file.filename
        }
    },
    // 删除头像
    delAvatarImgUpload: async (ctx,next)=>{
        const {filename} = ctx.request.body
        // 删除图片
        fs.unlink(path.join(__dirname ,`../public/avatarImgs/${filename}`),  (err)=>{})
        ctx.body = {
            code:200,
            msg:'删除成功'
        }
    },


    // 上传轮播图
    swiperImgUpload: async (ctx,next)=>{
        // 图片地址拼接
        const headPath = host +  '/swiperImgs/'+ctx.req.file.filename
        ctx.body = {
            code: 200,
            msg:'上传成功',
            fileUrl: headPath,
            filename:ctx.req.file.filename
        }
        
    },
    // 轮播图地址添加进数据库
    sureAddSwiper: async (ctx,next)=>{
        const {img_url,img_name,author} = ctx.request.body
        const create_time = new Date()
        await db.promise().query('insert into swiper(img_url, img_name, create_time,author ) values(?,?,?,?)',[img_url,img_name,create_time,author]).then(res=>{
            if(res[0].affectedRows ==1){
                ctx.body = {
                    code: 200,
                    msg:'添加成功',
                }
            }
        }).catch(err=>{
            ctx.body = {
                code: 200,
                msg:'添加失败'+err,
            }
        })
    },
    // 删除轮播图
    delSwiperUpload: async (ctx,next)=>{
        const {filename,swiperId} = ctx.request.body
        if(!swiperId){
            // 删除图片
            fs.unlink(path.join(__dirname ,`../public/swiperImgs/${filename}`),  (err)=>{
                console.log(err);
                ctx.body = {
                    code:201,
                    msg:"数据库图片删除失败"+err
                }
            })
            ctx.body = {
                code:200,
                msg:"服务器图片删除成功"
            }
        }else{
            // 删除图片
            fs.unlink(path.join(__dirname ,`../public/swiperImgs/${filename}`),  (err)=>{
                ctx.body = {
                    code:201,
                    msg:"数据库图片删除失败"+err
                }
            })
            // 删除数据库图片数据
            await db.promise().query('delete from swiper where id=?',[swiperId]).then(res=>{
                if(res[0].affectedRows == 1){
                    ctx.body = {
                        code:200,
                        msg:"数据库图片删除成功"
                    }
                }
            })
        }
        
        
    },
    // 获取轮播图
    getSwipers: async (ctx,next)=>{
        const pageObj = await paging(ctx)
        const startPage = pageObj.startPage
        const endPage = pageObj.endPage
        await db.promise().query('select * from swiper',[startPage,endPage]).then(async res=>{
            let total = res[0].length
            await db.promise().query('select * from swiper order by create_time desc limit ?,?',[startPage,endPage]).then(ress=>{
                    ctx.body = {
                        code:1000,
                        msg:'获取成功',
                        swipers:ress[0],
                        total:total
                    }
                
            }).catch(err=>{
                ctx.body = {
                    code:1001,
                    msg:'获取失败'+err
                }
            })
        }).catch(err=>{
            ctx.body = {
                code:1001,
                msg:'获取失败'+err
            }
        })
    },


    // 删除广告图
    delAdUpload: async (ctx,next)=>{
        const {filename,adId} = ctx.request.body
        if(!adId){
            // 删除图片
            fs.unlink(path.join(__dirname ,`../public/adImgs/${filename}`),  (err)=>{
                ctx.body = {
                    code:201,
                    msg:"数据库图片删除失败"+err
                }
            })
            ctx.body = {
                code:200,
                msg:"服务器图片删除成功"
            }
        }else{
            // 删除图片
            fs.unlink(path.join(__dirname ,`../public/adImgs/${filename}`),  (err)=>{
                ctx.body = {
                    code:201,
                    msg:"数据库图片删除失败"+err
                }
            })
            // 删除数据库图片数据
            await db.promise().query('delete from advert where id=?',[adId]).then(res=>{
                if(res[0].affectedRows == 1){
                    ctx.body = {
                        code:200,
                        msg:"数据库图片删除成功"
                    }
                }
            })
        }
       
    },
    // 上传广告图
    adImgUpload:async (ctx,next)=>{
        // 图片地址拼接
        const headPath =host +  '/adImgs/'+ctx.req.file.filename
        ctx.body = {
            code: 200,
            msg:'上传成功',
            fileUrl: headPath,
            filename:ctx.req.file.filename
        }
    },
    // 确认上传广告图
    sureAddAd:async (ctx,next)=>{
        const {img_url,img_name,author} = ctx.request.body
        const create_time = new Date()
        await db.promise().query('insert into advert(img_url, img_name, create_time,author ) values(?,?,?,?)',[img_url,img_name,create_time,author]).then(res=>{
            if(res[0].affectedRows ==1){
                ctx.body = {
                    code: 200,
                    msg:'添加广告成功',
                }
            }
        }).catch(err=>{
            ctx.body = {
                code: 200,
                msg:'添加广告失败'+err,
            }
        })
    },
    getAds:async (ctx,next)=>{
        const pageObj = await paging(ctx)
        const startPage = pageObj.startPage
        const endPage = pageObj.endPage
        await db.promise().query('select * from advert').then(async res=>{
            let total = res[0].length
            await db.promise().query('select * from advert order by create_time desc limit ?,?',[startPage,endPage]).then(ress=>{
                    ctx.body = {
                        code:1000,
                        msg:'获取成功',
                        ads:ress[0],
                        total:total
                    }
                
            }).catch(err=>{
                ctx.body = {
                    code:1001,
                    msg:'获取失败'+err
                }
            })
        }).catch(err=>{
            ctx.body = {
                code:1001,
                msg:'获取失败'+err
            }
        })
    },

    // 博客封面上传
    blogFaceLoad:async (ctx,next)=>{
        // 图片地址拼接
        const headPath =  host + '/blogFace/'+ctx.req.file.filename
        ctx.body = {
            code: 200,
            msg:'上传成功',
            fileUrl: headPath,
            filename:ctx.req.file.filename
        }
    },
    // 博客封面删除（服务器端）
    delBlogFaceLoad:async (ctx,next)=>{
        const {filename} = ctx.request.body
        // 删除图片
        fs.unlink(path.join(__dirname ,`../public/blogFace/${filename}`),  (err)=>{})
        ctx.body = {
            code:200,
            msg:'删除成功'
        }
    }
}